package com.jd.ty.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jd.ty.dao.constant.QueryConstants;
import com.jd.ty.dao.util.HqlCriterions;
import com.jd.ty.pojo.Dictionary;
import com.jd.ty.service.DictionaryService;
import com.jd.ty.service.exception.ServiceException;
import com.jd.ty.util.Pager;
import com.jd.ty.web.filter.DictionaryFilter;
import com.opensymphony.xwork2.ModelDriven;

public class DictionaryAction extends BaseAction implements ModelDriven<Dictionary> {

	private static final long serialVersionUID = -4018571604337249963L;

	private Long id;

	private Long parent;
	private Long nodeid;
	private DictionaryFilter filter;
	private List<Dictionary> dictionaryList;
	private Dictionary dictionary = new Dictionary();
	private DictionaryService dictionaryService;
	private String oper;

	@Override
	public String execute() throws Exception {
		dictionaryList = dictionaryService.getRootList();
		return SUCCESS;
	}

	public String list() {
		pager = getPagerObject();

		HqlCriterions hc = new HqlCriterions();
		if ("".equals(nodeid)||nodeid == null) {
			hc.and(QueryConstants.Dictionary.PARENT, null, QueryConstants.EQ);
		} else {
			hc.and(QueryConstants.Dictionary.PARENT + "." + QueryConstants.Dictionary.DICTIONARY_ID, nodeid,
					QueryConstants.EQ);
		}

		List<Dictionary> res = dictionaryService.getListByFiter(pager, hc.getHqlString());
		List<Map> diList = new ArrayList<Map>();
		for (Dictionary dictionary : res) {
			Map<String, Object> cell = new HashMap<String, Object>();
			cell.put("id", dictionary.getDictionaryId());
			cell.put("cell", dictionary.toList());
			diList.add(cell);
		}

		pager.setDataRows(diList);
		return SUCCESS;
	}
	public String children() {
		dictionaryList = dictionaryService.getListByParentId(parent);
		return SUCCESS;
	}
	public String update() throws ServiceException {
		try {
			
			if (oper.equals(Operation.edit.name())) {
				dictionary.setDictionaryId(id);
				dictionaryService.update( dictionary);
			} else if (oper.equals(Operation.add.name())) {
				dictionaryService.add(parent, dictionary);
			}else{
				dictionaryService.delete(id);
			}
		} catch (ServiceException e) {
			addActionError(e.getErrorMessage());
			return INPUT;
		}
		return SUCCESS;
	}




	public DictionaryFilter getFilter() {
		if (filter == null) {
			filter = new DictionaryFilter();
		}

		return filter;
	}

	public void setFilter(DictionaryFilter filter) {
		this.filter = filter;
	}

	public void setDictionaryService(DictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}

	public Pager getPager() {
		return pager;
	}



	public List<Dictionary> getDictionaryList() {
		return dictionaryList;
	}

	public void setDictionaryList(List<Dictionary> dictionaryList) {
		this.dictionaryList = dictionaryList;
	}

	public Dictionary getDictionary() {
		return dictionary;
	}

	public void setDictionary(Dictionary dictionary) {
		this.dictionary = dictionary;
	}

	@Override
	public Dictionary getModel() {
		return dictionary;
	}

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}



	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getParent() {
		return parent;
	}

	public void setParent(Long parent) {
		this.parent = parent;
	}

	public Long getNodeid() {
		return nodeid;
	}

	public void setNodeid(Long nodeid) {
		this.nodeid = nodeid;
	}



	enum Operation {
		add, edit, del
	}
}
